from playwright.sync_api import sync_playwright
import json
import os

tab_list = [
    {"title": "热搜", "tab": "realtime"},
    {"title": "小说", "tab": "novel"},
    {"title": "电影", "tab": "movie"},
    {"title": "电视剧", "tab": "teleplay"},
    {"title": "汽车", "tab": "car"},
    {"title": "游戏", "tab": "game"},
]

def write_to_json(data, filename):
    os.makedirs(os.path.dirname(filename), exist_ok=True)
    with open(filename, "w", encoding="utf-8") as f:
        json.dump(data, f, ensure_ascii=False, indent=4)
    print(f"数据已成功写入到 {filename}")


def get_list(playwright, tab, title):
    print(f"开始获取 {title}...")
    browser = playwright.chromium.launch(headless=True)
    page = browser.new_page()
    page.goto(f"https://top.baidu.com/board?tab={tab}")

    # 等待动态加载完成
    page.wait_for_selector(".content_1YWBm")

    # 获取内容
    items = page.query_selector_all(".content_1YWBm")
    data_list = []
    for item in items:
        try:
            title_name = item.query_selector("a.title_dIF3B div.c-single-text-ellipsis").inner_text()
            title_link = item.query_selector("a.title_dIF3B").get_attribute("href")
            data_list.append({"title": title_name.strip(), "link": title_link.strip()})
        except Exception as e:
            print(f"无法获取 {title} 的部分数据: {e}")

    filename = f"output/playwright/{title}.json"
    write_to_json(data_list, filename)
    print(f"完成获取 {title}. 一共获取到 {len(data_list)} 条数据。")
    browser.close()


with sync_playwright() as playwright:
    for item in tab_list:
        get_list(playwright, item["tab"], item["title"])
